package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import dao.OrderListDao;
import entiey.OrderList;
import util.JDBCUtil;
import util.ResultSetCreateObject;

public class OrderListDaoImpl implements OrderListDao,ResultSetCreateObject<OrderList>{

	@Override
	public int add(OrderList t) {
		int num=JDBCUtil.executeUpdate("insert into orderlist (orderNo,wid,vid,tid,mid,foodPrice,foodName,foodNum,orderTime,remark) values(?,?,?,?,?,?,?,?,now(),?)", t.getOrderNo(),t.getWid(),0,t.getTid(),t.getMid(),t.getFoodPrice(),t.getFoodName(),t.getFoodNum(),t.getRemark());
		return num;
	}

	@Override
	public int delete(int no) {
		int num=JDBCUtil.executeUpdate("delete from where orderlist no=?", no);
		return num;
	}

	@Override
	public int update(OrderList t) {
		int num=JDBCUtil.executeUpdate("update orderlist set(wid=?,tid=?,mid=?,orderListName=?,orderListPrice=?,orderListNum=?,orderTime=?,remark=?) where orderNo=?", t.getWid(),t.getTid(),t.getMid(),t.getFoodName(),t.getFoodPrice(),t.getFoodNum(),t.getRemark(),t.getOrderNo());
		return num;
	}

	@Override
	public List<OrderList> findAll() {
		return JDBCUtil.executeQuery("select * from orderlist", this);
	}

	@Override
	public OrderList findById(int id) {
		List<OrderList> list = JDBCUtil.executeQuery("select * from orderlist where orderNo=?", this,id);
		OrderList list2=new OrderList();
		if(list.size()>0) {
			list2=list.get(0);
		}
		return list2;
	}

	@Override
	public OrderList create(ResultSet rs) {
		OrderList orderList=new OrderList();
		try {
			orderList.setOrderNo(rs.getString("orderNo"));
			orderList.setWid(rs.getInt("wid"));
			orderList.setTid(rs.getInt("tid"));
			orderList.setMid(rs.getInt("mid"));
			orderList.setFoodName(rs.getString("orderListName"));
			orderList.setFoodPrice(rs.getDouble("orderListPrice"));
			orderList.setFoodNum(rs.getInt("orderListNum"));
			orderList.setOrderTime(rs.getTimestamp("orderTime"));
			orderList.setRemark(rs.getString("remark"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return orderList;
	}

	

}
